package com.software.dao;

import com.software.model.Product;
import com.software.utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ProductDAO {

    // 插入商品
    public void addProduct(Product product) {
        String sql = "INSERT INTO products (productId, productName, productPrice, productDesc, productImage, typeId, quantity, recommend) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, product.getProductId());
            pstmt.setString(2, product.getProductName());
            pstmt.setDouble(3, product.getProductPrice());
            pstmt.setString(4, product.getProductDesc());
            pstmt.setString(5, product.getProductImage());
            pstmt.setInt(6, product.getTypeId());
            pstmt.setDouble(7, product.getQuantity());
            pstmt.setBoolean(8, product.getRecommend());

            pstmt.executeUpdate(); // 执行插入操作
            System.out.println("商品添加成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 根据商品ID查找商品
    public Product getProductById(String productId) {
        String sql = "SELECT * FROM products WHERE productId = ?";
        Product product = null;

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, productId);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                product = new Product(
                        rs.getString("productId"),
                        rs.getString("productName"),
                        rs.getDouble("productPrice"),
                        rs.getString("productDesc"),
                        rs.getString("productImage"),
                        rs.getInt("typeId"),
                        rs.getDouble("quantity"),
                        rs.getBoolean("recommend")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return product;
    }

    // 获取所有商品列表
    public List<Product> getAllProducts() {
        List<Product> productList = new ArrayList<>();
        String sql = "SELECT * FROM products";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Product product = new Product(
                        rs.getString("productId"),
                        rs.getString("productName"),
                        rs.getDouble("productPrice"),
                        rs.getString("productDesc"),
                        rs.getString("productImage"),
                        rs.getInt("typeId"),
                        rs.getDouble("quantity"),
                        rs.getBoolean("recommend")
                );
                productList.add(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return productList;
    }

    // 更新商品信息
    public void updateProduct(Product product) {
        String sql = "UPDATE products SET productName = ?, productPrice = ?, productDesc = ?, productImage = ?, typeId = ?, quantity = ?, recommend = ? WHERE productId = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, product.getProductName());
            pstmt.setDouble(2, product.getProductPrice());
            pstmt.setString(3, product.getProductDesc());
            pstmt.setString(4, product.getProductImage());
            pstmt.setInt(5, product.getTypeId());
            pstmt.setDouble(6, product.getQuantity());
            pstmt.setBoolean(7, product.getRecommend());
            pstmt.setString(8, product.getProductId());

            pstmt.executeUpdate();
            System.out.println("商品信息更新成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 根据商品ID删除商品
    public void deleteProduct(String productId) {
        String sql = "DELETE FROM products WHERE productId = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, productId);
            pstmt.executeUpdate();
            System.out.println("商品删除成功！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
